package com.lxk.service.impl;

import com.lxk.dao.AsMyMapper;
import com.lxk.dao.PointGoodsMapper;
import com.lxk.pojo.PointGoods;
import com.lxk.pojo.PointGoodsExample;
import com.lxk.service.PointService;
import com.lxk.util.exception.GlobalException;
import com.lxk.vo.PageModel;
import com.lxk.vo.result.CodeMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author JohnSoybean
 * @create 2020/4/7 15:43
 * @description
 */
@Service
@Transactional
public class PointServiceImpl implements PointService {

    @Autowired
    private PointGoodsMapper pgm;
    @Autowired
    private AsMyMapper mm;

    public PointGoods selPointGoodsById(Integer pointGoodsId) {
        PointGoods pointGoods = pgm.selectByPrimaryKey(pointGoodsId);
        return pointGoods;
    }

    public Integer getPointGoodsTotalPages(Integer num) {
        PointGoodsExample example = new PointGoodsExample();
        PointGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andIsUpperEqualTo(1);
        Integer count = (int)pgm.countByExample(example);
        Integer totalPages = count%num==0 ? count/num : count/num+1;
        return totalPages;
    }

    public PageModel<PointGoods> selPointGoodsPage(Integer pageIndex, Integer num, Integer totalPages, Integer sort, Integer order) {
        int start = (pageIndex-1)*num;
        List<PointGoods> list = mm.selPointGoodsPage(start, num, sort, order);
        PageModel<PointGoods> model = new PageModel<PointGoods>();
        model.setList(list);
        model.setNum(num);
        model.setPageIndex(pageIndex);
        model.setTotalPages(totalPages);
        return model;
    }

    public Integer addPointGoods(PointGoods pointGoods, Integer isUpper) {
        int ins = pgm.insertSelective(pointGoods);
        if(ins==0) {
            throw new GlobalException(CodeMsg.ADD_GOODS_ERROR);
        }
        return 1;
    }

    public Integer modifyPointGoodsInfo(PointGoods pointGoods) {
        int upd = pgm.updateByPrimaryKeySelective(pointGoods);
        return upd;
    }

    public String getPointGoodsDetailText(Integer pointGoodsId) {
        PointGoods pointGoods = pgm.selectByPrimaryKey(pointGoodsId);
        if(pointGoods==null) {
            throw new GlobalException(CodeMsg.NO_SUCH_GOODS);
        }
        String pointGoodsDetail = pointGoods.getPointGoodsDetail();
        return pointGoodsDetail;
    }

    public Integer updPointGoodsDetailText(Integer pointGoodsId, String pointGoodsDetail) {
        PointGoods pointGoods = pgm.selectByPrimaryKey(pointGoodsId);
        if (null==pointGoods) {
            throw new GlobalException(CodeMsg.NO_SUCH_GOODS);
        }
        pointGoods.setPointGoodsDetail(pointGoodsDetail);
        int upd = pgm.updateByPrimaryKeyWithBLOBs(pointGoods);
        if(upd==0) {
            throw new GlobalException(CodeMsg.MYSQL_CRUD_FAIL);
        }
        return 1;
    }

    public Integer deletePointGoods(Integer pointGoodsId) {
        PointGoodsExample example = new PointGoodsExample();
        PointGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andPointGoodsIdEqualTo(pointGoodsId);
        List<PointGoods> list = pgm.selectByExample(example);
        if (list.size()==0) {
            throw new GlobalException(CodeMsg.NO_SUCH_GOODS);
        }
        PointGoods pointGoods = list.get(0);
        pointGoods.setIsUpper(2);
        int upd = pgm.updateByPrimaryKeySelective(pointGoods);
        return upd;
    }
}
